﻿using DongFangGuoKai.WMSPlatform.Commn.ApiResultModule;
using DongFangGuoKai.WMSPlatform.Commn.ExtendModule;
using DongFangGuoKai.WMSPlatform.Service.InventoryOrderDetail;
using DongFangGuoKai.WMSPlatform.Service.InventoryOrderDetail.DTO;
using DongFangGuoKai.WMSPlatform.Service.User;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Numerics;
using System.Security.Claims;

namespace DongFangGuoKai.WMSPlatform.WebApi.Controllers.FiguringManage
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class InventoryOrderController : BaseController
    {
        private readonly ITInventoryOrderDetailService tInventoryOrderDetailService;
        private readonly IUserService userService;
        private readonly ILogger<InventoryOrderController> inventoryorderlogger;

        public InventoryOrderController(ITInventoryOrderDetailService tInventoryOrderDetailService,
            IUserService userService,
            ILogger<InventoryOrderController> inventoryorderlogger
            )
        {
            this.tInventoryOrderDetailService = tInventoryOrderDetailService;
            this.userService = userService;
            this.inventoryorderlogger = inventoryorderlogger;
        }


        /// <summary>
        /// 盘点单添加（海宁）
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddStocktakingOrder(InventoryIssuanceModel inventoryIssuanceModel)
        {
            if (!ModelState.IsValid)
            {
                inventoryorderlogger.LogError("添加盘点单失败，传过来数据格式不正确." + "入参，" + inventoryIssuanceModel.ToJson());
                return Failed("传过来数据格式不正确",400);
            }

            try
            {
                bool pandian = tInventoryOrderDetailService.AddStocktakingOrder(inventoryIssuanceModel, out string message);
                if (!pandian)
                {
                    inventoryorderlogger.LogError("添加盘点单失败，" + message + "入参，" + inventoryIssuanceModel.ToJson());
                    return Failed(message);
                }
                inventoryorderlogger.LogInformation("添加盘点单成功，" + message + "入参，" + inventoryIssuanceModel.ToJson());
                return Success("添加成功！");
            }
            catch (Exception ex)
            {
                return Failed(ex.Message);
            }
        }


        /// <summary>
        /// 根据盘点单号回传状态
        /// </summary>
        /// <param name="ordernum"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdateStocktakingOrderStatus(string ordernum)
        {
            if (string.IsNullOrEmpty(ordernum))
            {
                return Failed("参数有误！", 400);
            }
            bool IsOk = tInventoryOrderDetailService.UpdateStocktakingOrderStatus(ordernum, out string message);

            if (IsOk)
            {
                inventoryorderlogger.LogInformation("根据盘点单号回传状态成功，" + message + "入参，" + ordernum);
                return Success(message);
            }
            inventoryorderlogger.LogError("根据盘点单号回传状态失败，" + message + "入参，" + ordernum);
            return Failed(message);
        }

        /// <summary>
        /// pda获取盘点单数据
        /// </summary>
        /// <param name="orderNo"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetStocktakingOrderList(string? orderNo, int pageindex = 1, int pagesize = 10)
        {
            int totalCount;
            var stocktakingOrders = tInventoryOrderDetailService.GetStocktakingOrderList(orderNo, pageindex, pagesize, out totalCount);
            if (stocktakingOrders == null)
            {
                return Failed("未查询到盘点信息", 200);
            }
            return Success(stocktakingOrders, totalCount, "查询成功");
        }

        /// <summary>
        /// pc查询盘点数据
        /// </summary>
        /// <param name="orderNo"></param>
        /// <param name="status"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetStocktakingOrderListPC(string? orderNo, int? status, int pageindex = 1, int pagesize = 10)
        {
            int totalCount;
            var stocktakingOrders = tInventoryOrderDetailService.GetStocktakingOrderListPC(orderNo, status, pageindex, pagesize, out totalCount);
            if (stocktakingOrders == null)
            {
                return Success("没有执行的任务");
            }
            return Success(stocktakingOrders, totalCount, "查询成功");
        }



        /// <summary>
        /// pda盘点 
        /// </summary>
        /// <param name="inventoryOrderNumberDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateStocktakingOrder(InventoryOrderNumberDTO inventoryOrderNumberDTO)
        {
            string message;
            bool IsOK = tInventoryOrderDetailService.UpdateStocktakingOrder(inventoryOrderNumberDTO, out message);
            if (IsOK)
            {
                inventoryorderlogger.LogInformation("pda盘点成功，" + message + "入参，" + inventoryOrderNumberDTO.ToJson());
                return Success(message);
            }
            inventoryorderlogger.LogError("pda盘点失败，" + message + "入参，" + inventoryOrderNumberDTO.ToJson());
            return Failed( message);
        }

        /// <summary>
        /// 用户显示
        /// </summary>
        /// <param name="uname"></param>
        /// <param name="rid"></param>
        /// <param name="state"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetUserData(string? uname, int rid = 0, int state = 3, int pageIndex = 1, int pageSize = 10)
        {
            int totaclt;
            var resoty = userService.GetUserList(uname, rid, state, pageIndex, pageSize, out int totalCount).Select(n => new { n.Id, n.UserName, n.RealName });
            if (resoty != null)
            {
                return Success(resoty, "查询成功");
            }
            return Failed("查询失败");
        }
    }
}
